package me.stephenj.contactsweb.mapper;

import me.stephenj.contactsweb.model.TestQueryVO;
import me.stephenj.contactsweb.model.TestVO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public interface ContactsMapper {

    @Select("<script>SELECT * FROM `contacts` WHERE 1=1 " +
            "<if test='lastname != null'> AND lastname = #{lastname} </if>" +
            "<if test='firstname != null'> AND firstname = #{firstname} </if>" +
            "<if test='phone1 != null'> AND phone1 = #{phone1} </if>" +
            "<if test='email != null'> AND email = #{email} </if>" +
            "<if test='address != null'> AND address = #{address} </if>" +
            "<if test='qq != null'> AND qq = #{qq} </if>" +
            "<if test='wechat != null'> AND wechat = #{wechat} </if>" +
            "</script>")
    List<TestVO> getAll(TestQueryVO testQueryVO);

    @Select("SELECT COUNT(*) FROM `contacts`")
    int getCount();

    @Insert("INSERT INTO `contacts` (`avatar`, `lastname`, `firstname`, `phone1`, `phone2`, `phone3`, `email`, `address`, `qq`, `wechat`, `birthday`) VALUES (#{avatar}, #{lastname}, #{firstname}, #{phone1}, #{phone2}, #{phone3}, #{email}, #{address}, #{qq}, #{wechat}, #{birthday})")
    int insert(TestVO vo);

    @Delete("DELETE FROM contacts WHERE ID=#{ID}")
    int delete(int ID);

    @Update("UPDATE `contacts` SET `avatar`=#{avatar}, `lastname`=#{lastname}, `firstname`=#{firstname}, `phone1`=#{phone1}, `phone2`=#{phone2}, `phone3`=#{phone3}, `email`=#{email}, `address`=#{address}, `qq`=#{qq}, `wechat`=#{wechat}, `birthday`=#{birthday} WHERE ID=#{ID}")
    int update(TestVO vo);
}
